﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Factory
{
    public class RegraDAO
    {
        public Regra ObterRegras()
        {
            try
            {
                DAOBase m_cnn = new DAOBase();

                #region Query
                string query =
                    @"SELECT
                       LimiteFactoryClienteComum
                      ,LimiteFactoryClienteEspecial
                      ,LimitePorCpf
                      ,LimitePorCheque
                      ,PorcentagemEmprestimo
                      ,TaxaCobradaCheque
                  FROM Configuracoes";
                #endregion

                SqlCommand cmd = new SqlCommand(query, m_cnn.CreateConnection());
                SqlDataReader dr = cmd.ExecuteReader();

                IList<Regra> regra = ReaderToObjects(dr);

                return regra.FirstOrDefault();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return null;
            }
        }

        public static IList<Regra> ReaderToObjects(IDataReader reader)
        {
            if (reader.IsClosed)
            {
                throw new ArgumentException("O DataReader fornecido já estava fechado.");
            }
            
            int LimiteFactoryClienteComum_pos = reader.GetOrdinal("LimiteFactoryClienteComum");
            int LimiteFactoryClienteEspecial_pos = reader.GetOrdinal("LimiteFactoryClienteEspecial");
            int LimitePorCpf_pos = reader.GetOrdinal("LimitePorCpf");
            int LimitePorCheque_pos = reader.GetOrdinal("LimitePorCheque");
            int Porcentagem_pos = reader.GetOrdinal("PorcentagemEmprestimo");
            int TaxaCobradaCheque_pos = reader.GetOrdinal("TaxaCobradaCheque");

            IList<Regra> result = new List<Regra>();

            while (reader.Read())
            {
                Regra c = new Regra()
                {
                    PorcentagemEmprestimo = reader.GetDouble(Porcentagem_pos),
                    LimiteFactoryClienteComum = reader.GetDouble(LimiteFactoryClienteComum_pos),
                    LimitePorCheque = reader.GetDouble(LimitePorCheque_pos),
                    LimitePorCpf = reader.GetDouble(LimitePorCpf_pos),
                    LimiteFactoryClienteEspecial = reader.GetDouble(LimiteFactoryClienteEspecial_pos),
                    TaxaCobradaPorCheque = reader.GetDouble(TaxaCobradaCheque_pos),
                };

                result.Add(c);
            }
            return result;
        }

        public int atualizarRegras(Regra regra)
        {            
            int resultado = 0;

         
                DAOBase m_cnn = new DAOBase();

                #region query
                string query = @"UPDATE Configuracoes
                                   SET LimiteFactoryClienteComum =@LimiteFactoryClienteComum
                                      ,LimitePorCpf = @LimitePorCpf
                                      ,LimitePorCheque = @LimitePorCheque
                                      ,LimiteFactoryClienteEspecial = @LimiteFactoryClienteEspecial
                                      ,PorcentagemEmprestimo= @PorcentagemEmprestimo
                                      ,TaxaCobradaCheque=@TaxaCobradaCheque";
                #endregion

                SqlCommand cmd = new SqlCommand(query, m_cnn.CreateConnection());

                //Parametros de Usuario
                cmd.Parameters.Add(new SqlParameter("LimiteFactoryClienteComum", regra.LimiteFactoryClienteComum));
                cmd.Parameters.Add(new SqlParameter("LimitePorCpf", regra.LimitePorCpf));
                cmd.Parameters.Add(new SqlParameter("LimitePorCheque", regra.LimitePorCheque));
                cmd.Parameters.Add(new SqlParameter("LimiteFactoryClienteEspecial", regra.LimiteFactoryClienteEspecial));
                cmd.Parameters.Add(new SqlParameter("PorcentagemEmprestimo", regra.PorcentagemEmprestimo));
                cmd.Parameters.Add(new SqlParameter("TaxaCobradaCheque", regra.TaxaCobradaPorCheque));

                resultado = cmd.ExecuteNonQuery();

                m_cnn.DestroyConnection();

                return resultado;
            
        }
    }
}
